package extraction;

import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;

import opennlp.tools.util.InvalidFormatException;
import WikiPT.Relations;

public class ExtractFeatures implements Runnable {
	
	private final long n_split;
	private final Features f;
	private final boolean symmetric;

	public ExtractFeatures(int n, boolean symmetric) throws InvalidFormatException, FileNotFoundException, IOException {
		this.n_split=n;
		f = new Features();
		this.symmetric = symmetric;
	}
	
	@Override
	public void run() {		
		String file = "split_"+n_split+".txt";
		PrintWriter outFile = null;
		try {
			outFile = new PrintWriter(new FileWriter("train-data-wikipt_"+n_split+".txt"));
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		System.out.println("Launching Thread " + n_split);
		BufferedReader input = null;
		try {
			input = new BufferedReader(new FileReader(file));
		} catch (FileNotFoundException e3) {
			// TODO Auto-generated catch block
			e3.printStackTrace();
		}
		String aux = null;
		String sentence = null;
		String type_original = null;
		String e1 = null;
		String e2 = null;
		String type = null;
		try {
			while ((aux = input.readLine()) != null) {
				if (aux.startsWith("SENTENCE")) {
					sentence = aux.split(": ")[1];			
					aux = input.readLine();
					if (aux.equals("")) aux = input.readLine();
					if (aux.startsWith("MANUALLY CHECKED : IGNORE")) continue;
					while (!aux.startsWith("*")) {
						try {
							if (aux.startsWith("ENTITY1")) e1 = aux.split(": ")[1].trim();
							if (aux.startsWith("ENTITY2")) e2 = aux.split(": ")[1].trim();
							if (aux.startsWith("REL TYPE")) type_original = aux.split(": ")[1];
						} catch (Exception e) {
							break;												
						}
						aux = input.readLine();
					}
					if (type_original != null && e1 != null && e2 != null) {
						sentence = sentence.replaceAll("&nbsp;", "").replaceAll("&mdash;", "—").replaceAll("&ndash", "–").replaceAll("&bull;", "•");
						sentence = sentence.replaceAll("\\[?URLTOKEN\\s?([A-Za-z0-9íÍÌìàÀáÁâÂâÂãÃçÇéÉêÊóÓõÕôÔúÚüÜ\\.\\s,\\+\\(\\)\\-]+)?\\]?", "");
						//fix SentenceDetector errors...
						sentence = sentence.replaceAll(" ca\\. "," ca ").replaceAll(" etc\\. ", " etc ").replaceAll("\\(c\\. ", "(c ").replaceAll(" c\\. ", " c ");				
						sentence = sentence.replaceAll(" Mrs\\. "," Mrs  ").replaceAll("Ph\\.D\\.", "PhD").replaceAll("LL\\.D\\.","LLD").replaceAll("Sc\\.D\\.","ScD").replaceAll(" Mr\\. "," Mr ");				
						sentence = sentence.replaceAll("([0-9]+)\\.([0-9]+)\\.([0-9]+)\\.([0-9]+)","$1 $2 $3 $4");
						sentence = sentence.replaceAll("([0-9]+)\\.([0-9]+)\\.([0-9]+)","$1 $2 $3");
						sentence = sentence.replaceAll("([0-9]+)\\.([0-9]+)","$1 $2");
						sentence = sentence.replaceAll(" Lei nº\\. "," Lei nº ").replaceAll(" n°\\. ", " nº ").replaceAll(" nº\\. ", "  nº ").replaceAll("\\(n. ", "(nº ");
						sentence = sentence.replaceAll(" S\\.A\\. "," SA ").replaceAll("Inc\\.","Inc");
						String result = Relations.changeRelationType(type_original, e1, e2);
						String[] data = result.split("\\|");
						type = data[0];
						e1 = data[1];
						e2 = data[2];
						if (!type.equalsIgnoreCase("ignore")) this.f.processRelations(sentence,e1,e2,type_original,type,outFile,symmetric);											
					}
				}
			}
			input.close();
			outFile.close();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		System.out.println("Thread " + n_split + " finished");
	}
}